﻿using ApplicationBase.Core.Context;
using MediatR;
using WebAppTemplate.CSR.Repository.DbContexts;
using WebAppTemplate.CSR.Repository.Entities;

namespace WebAppTemplate.CSR.Service.Commands
{
    public record AddDemoCommand(string Value) : IRequest<bool>;

    public class AddDemoCommandHandler : IRequestHandler<AddDemoCommand, bool>
    {
        private readonly AppDbContext _dbContext;
        private readonly IServiceContext _serviceContext;
        public AddDemoCommandHandler(AppDbContext dbContext, IServiceContext serviceContext) 
            => (_dbContext, _serviceContext) = (dbContext, serviceContext);

        public async Task<bool> Handle(AddDemoCommand request, CancellationToken cancellationToken)
        {
            var demo = new Demo { Id = _serviceContext.GenerateId(), Value = request.Value };
            await _dbContext.Demo.AddAsync(demo, cancellationToken);

            return true;
        }
    }
}
